package com.jvmstat.monitor;

import sun.jvmstat.monitor.MonitoredHost;
import sun.jvmstat.monitor.MonitoredVm;
import sun.jvmstat.monitor.MonitoredVmUtil;
import sun.jvmstat.monitor.VmIdentifier;

import java.util.HashSet;
import java.util.Set;

public class MonitoredHostDemo {

    /**
     * 查看本机所有jvm对应的pid
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // 获取监控主机
        MonitoredHost local = MonitoredHost.getMonitoredHost("localhost");
//        MonitoredHost local = MonitoredHost.getMonitoredHost("134.96.162.183");
        // 取得所有在活动的虚拟机集合
        Set<?> vmlist = new HashSet<Object>(local.activeVms());
        // 遍历集合，输出PID和进程名
        for(Object process : vmlist) {
            MonitoredVm vm = local.getMonitoredVm(new VmIdentifier("//" + process));
            // 获取类名
            String processname = MonitoredVmUtil.mainClass(vm, true);
            System.out.println(process + " ------> " + processname);
        }
    }

}
